package com.application.TextNode;


import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;



import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

public abstract class INodeText {
	protected long id;
	protected String text;
	protected String title;
	public static final String SPLIT_TEXT = "#";
	private BoardViewTN boardView;
	protected float x = 0;
	protected float y = 0;
	protected  int heigh = 65;
	protected  int width = 290;
	protected String user;
	int depth=0;
	int mcolor = Color.GRAY;
	public abstract void add(INodeText n);
	public abstract void remove(INodeText n);
	public abstract INodeText getById(long id);
	public abstract ArrayList<INodeText> getChildren();
	public abstract INodeText getCollision(float x,float y);
	public abstract void moveAll(float moveX, float moveY);
	public abstract void autoArrangeTextNode();
	public abstract int getSumChildren();
	public abstract int getNodeHeight();
	public abstract Element toXmlText(Document doc);
	public abstract INodeText readXML(Element element);
	public abstract boolean clearChildren();
	protected INodeText(){
	}
	protected INodeText(long id, String title, String text, float x,float y, String user){
		this.id=id;
		this.text=text;
		this.title=title;
		this.x=x;
		this.y=y;
		this.user=user;
	}
	protected void onDraw(Canvas canvas) {
		ArrayList<String> lines = Split(this.text, 30);
		ArrayList<String> linesTitle = Split(this.title, 30);
//		if (lines.size() >= 3)
//			heigh = 20 + 15 * lines.size();

		RectF rect = new RectF(x, y, x + width , y + heigh);
		int space = 1;
		RectF border = new RectF(x - space, y - space, x + width + space, y
				+ heigh + space);

		Paint p1 = new Paint();
		p1.setAntiAlias(true);
		p1.setStrokeWidth(3);
		
		//p1.setARGB(150, 254, 249, 246);
		p1.setColor(Color.rgb(254, 249, 246));
//			p1.setARGB(150, 79, 129, 189);
		canvas.drawRect(rect, p1);

		Rect bounds = new Rect();
		p1.setColor(Color.BLACK);
		String t = linesTitle.get(0);
		if(lines.size()>1)
			t+="...";
		canvas.drawText(t, x + 5, y + 13, p1);
		canvas.drawText("by ", x + 5, y + 25, p1);
		p1.setColor(Color.BLUE);
		canvas.drawText(this.user + ":", x + 23, y + 25, p1);
		p1.setColor(Color.BLACK);
		canvas.drawText(" - 25-05-2012,07:09 PM ", x + 90, y + 25, p1);
		p1.setColor(this.mcolor);
		p1.setStrokeWidth(1);
		canvas.drawLine(x+3, y+30, x+width, y+30, p1);
		p1.setColor(Color.BLACK);
		String s=lines.get(0);
		if(lines.size()>1)
			s+="...";
		canvas.drawText(s, x + 20, y + 47 + 0 * 15, p1);

		
		Paint p = new Paint();
		p.setStrokeWidth(2);
		p.setStyle(Paint.Style.STROKE);
		p.setAntiAlias(true);
		
		p.setColor(this.mcolor);
//		p.setColor(Color.rgb(156, 93, 138));
//		canvas.drawRoundRect(border, 10, 10, p);
		canvas.drawRect(border, p);
	}
	
	public ArrayList<String> Split(String text, int len) {
		String[] m = text.split(" ");
		String[] line = new String[m.length];
		if (m.length > 0) {
			int n = m.length;
			int j = 0;
			for (int i = 0; i < n; i++) {
				if (m[i].length() >= len) {
					line[j] = m[i];
					j++;
				} else {
					if (line[j] == null) {
						line[j] = m[i];

					} else {
						if ((line[j] + " " + m[i]).length() > len) {
							line[j + 1] = m[i];
							j++;
						} else {
							line[j] += " " + m[i];
							// j++;
						}
					}
				}
			}
		}
		ArrayList<String> rs = new ArrayList<String>();
		for (int i = 0; i < line.length; i++) {
			if (line[i] != null) {
				rs.add(line[i]);
			}
		}
		return rs;
		// return rs.toArray(java.lang.String);
	}
	
	public  String toParse(){
		StringBuffer buffer=new StringBuffer();
		buffer.append(this.id+SPLIT_TEXT);
		buffer.append(this.title+SPLIT_TEXT);
		buffer.append(this.text+SPLIT_TEXT);
		buffer.append(this.x+SPLIT_TEXT);
		buffer.append(this.y+SPLIT_TEXT);
		buffer.append(this.user+SPLIT_TEXT);
		buffer.append(this.depth+SPLIT_TEXT);
		buffer.append(this.mcolor+SPLIT_TEXT);
		
		return buffer.toString();
	}
	public  void parse(String s){
		String []m=s.split(INodeText.SPLIT_TEXT);
		this.id=Long.parseLong(m[0]);
		this.title=m[1];
		this.text=m[2];
		this.x=Float.parseFloat(m[3]);
		this.y=Float.parseFloat(m[4]);
		this.user=m[5];
		this.depth=Integer.parseInt(m[6]);
		this.mcolor=Integer.parseInt(m[7]);
	}
	
	protected boolean isCollision(float x, float y) {
		return (x > this.x && x < this.x + width) && (y > this.y && y < this.y + heigh);
	}
	protected void setText(String newText){
		this.text=newText;
	}
	protected void setPostion(float x, float y){
		this.x=x;
		this.y=y;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public float getX() {
		return x;
	}
	public void setX(float x) {
		this.x = x;
	}
	public float getY() {
		return y;
	}
	public void setY(float y) {
		this.y = y;
	}
	public int getHeigh() {
		return heigh;
	}
	public void setHeigh(int heigh) {
		this.heigh = heigh;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public int getDepth() {
		return depth;
	}
	public void setDepth(int depth) {
		this.depth = depth;
	}
	public int getMcolor() {
		return mcolor;
	}
	public void setMcolor(int mcolor) {
		this.mcolor = mcolor;
	}
	public String getText() {
		return text;
	}
	
	
}
